package cn.edu.scau.cmi.wangjushen.client;

import cn.edu.scau.cmi.wangjushen.singleton.Chairman;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class SingletonClient extends Application{
	
	Label resultLabel0 = new Label();
	Label resultLabel1 = new Label();
	Label resultLabel2 = new Label();
	boolean more_than_one = false;
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		GridPane pane = new GridPane();
		Button creaButton = new Button("create");
		TextField inputField = new TextField();
		creaButton.setOnAction(e-> showResult(inputField.getText()));
		pane.setAlignment(Pos.CENTER);
		pane.setPadding(new Insets(12,12,12,12));
		pane.setVgap(10);
		pane.setHgap(10);
		pane.add(new Label("Chairman's Name:"), 0, 0);
		pane.add(inputField, 1, 0);
		pane.add(creaButton, 1, 1);
		pane.add(resultLabel0, 1, 2);
		pane.add(resultLabel1, 1, 3);
		pane.add(resultLabel2, 1, 4);
		Scene scene = new Scene(pane, 600, 200);
		primaryStage.setTitle("单例与多例模式");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	private void showResult(String name) {
		Chairman chairman = Chairman.getInstance(name);
		String objectString;
		if(chairman != null) {
			if(more_than_one) {
				resultLabel0.setText("You have created a chairman object");
			}
			else {
				resultLabel0.setText("There is already a chairman");
				more_than_one = true;
			}
			objectString = chairman.toString();
		}
		else {
			objectString = "Please input the name in correct format";
		}
		resultLabel1.setText(objectString);
		resultLabel2.setText("主席名"+chairman.getName());
	}
	
	public static void main(String[] args) {
		Application.launch(args);
	}

	
}
